//
//  LZMyCouponsListPresenter.m
//  laziz_Consumer
//
//  Created by ZhaoDongBo on 2016/12/15.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZMyCouponsListPresenter.h"
#import "NetworkServiceManager.h"
#import "LZUserModel.h"
#import "LZMyRestaurantCoupons.h"
#import "LZMyCouponsModel.h"
#import "MJExtension.h"
#import "LZConst.h"

@implementation LZMyCouponsListPresenter

+ (void)getMyCouponsList:(NSInteger)tag
                 pageNum:(NSInteger)pageNum
               pageLimit:(NSInteger)pageLimit
           resultSuccess:(void (^)(NSMutableArray *, int, int, int, int, int))successBlock
            resultFailed:(void (^)(NSString *))failedBlock
{
    NSString * urlPath = [NSString stringWithFormat:@"%@/clientcustomerCoupons/listMyPage", customer];
    
    NSMutableDictionary * param_Dict = [NSMutableDictionary dictionary];
    [param_Dict setObject:[LZUserModel Instance].userToken forKey:@"token"];
    [param_Dict setObject:[NSNumber numberWithInteger:tag] forKey:@"status"];
    [param_Dict setObject:[NSNumber numberWithInteger:pageNum] forKey:@"cpage"];
    [param_Dict setObject:[NSNumber numberWithInteger:pageLimit] forKey:@"limit"];
    
    DLog(@"%@", param_Dict);
    
    [[NetworkServiceManager manager] executePostTask:urlPath parameters:param_Dict success:^(NSURLSessionDataTask *task, id resultData) {
        
        DLog(@"%@", resultData);
        
        NSDictionary *dic = (NSDictionary *)resultData;
        
        if ([dic[@"statusCode"] integerValue] == kCodeSuccesss) {
            
            int unUsedNum = [resultData[@"data"][@"unUsedNum"] intValue];
            int usedNum = [resultData[@"data"][@"usedNum"] intValue];
            int expiredNum = [resultData[@"data"][@"expiredNum"] intValue];
            int processNum = [resultData[@"data"][@"processNum"] intValue];
            
            int totlePageTemp = [resultData[@"data"][@"promotionList"][@"totalPages"] intValue];
            int totalCountTemp = [resultData[@"data"][@"promotionList"][@"totalCount"] intValue];

            NSMutableArray *listTemp = [NSMutableArray array];
            
            if (totalCountTemp > 0) {
                NSArray *arrTemp = resultData[@"data"][@"promotionList"][@"list"];
                for (int i = 0; i < arrTemp.count; i ++) {
                    NSDictionary *dicTemp = arrTemp[i];
                    LZMyRestaurantCoupons *m = [LZMyRestaurantCoupons mj_objectWithKeyValues:dicTemp];
                    if (m) {
                        [listTemp addObject:m];
                    }
                }
            }

            if (successBlock) {
                successBlock(listTemp, unUsedNum, usedNum, expiredNum, processNum, totlePageTemp);
            }
            
        } else {
            NSString *str = dic[@"statusCode"];
            [LZBasePresenter checkAuthorStatus:str];
            if (failedBlock) {
                failedBlock(str);
            }
        }
        
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        if (failedBlock) {
            failedBlock(@"网络错误,请稍后再试");
        }
    }];
}
@end
